function [WM,order]=CMoment(D,x0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               CMoment.m
%
% Author:             Tasos Kalandrakis
%
% Description:        Evaluates selection of moment function m^C for moment
%                     inequalities (M_C) with data D, x=x0.
%                     Only consecutive triplets are used.
%                     Used to test x=x0 under (D), (C), (E).
%
% Created:            Oct - 6 - 2016
%
% Last Modified:      Feb - 1  - 2018
%
% Language:           MATLAB
%
% Related Reference: Tasos Kalandrakis. "One-dimensional scaling without 
%                    without apologies," forthcoming, Journal of
%                    Politics
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[~,order]=sort(x0);
D=D(:,order);
x0=x0(order);
[N,P]=size(D);
Triplets=(P-2);
WM=zeros(N,Triplets);

col=1;
for r1=1:P-2
    r2=r1+1;
    r3=r2+1;
    WM(:,col)=(x0(r3)-x0(r2))*(D(:,r2)-D(:,r1))-(x0(r2)-x0(r1))*(D(:,r3)-D(:,r2));
    col=col+1;
end

end